<?php

namespace web\common\command;

use web\common\service\Credential;
use web\common\service\TronKit;
use web\common\service\TronApi;
use addons\member\model\MemberAccountModel;
use addons\shop\model\ShopOrder;
use web\common\model\sys\SysParameterModel;
use think\Config;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\input\Argument;
use think\console\Output;
use think\Exception;
use think\Db;

class Trc20 extends Command{

    /**
     * 定义命令
     */
    protected function configure(){
        $this->setName('Trc20')->setDescription('RUN ETH Data RPC API');
        $this->addArgument('action', Argument::REQUIRED, "The name of the function");
        //参数2
        $this->addArgument('param', Argument::OPTIONAL, "The name of the param");
    }
    
    protected function execute(Input $input, Output $output) {
        $action = $input->getArgument('action');
        $param = $input->getArgument('param');
        if (method_exists($this,$action)) {
            $this->$action($param);
        }
        else{
            echo "method is not exists\n";
        }
    }


    public function transOut(){
        // $re = $this->sendTrx(8.56709,'TS4ZqZ8cG8pAuASKS5Dr8Ue8AtrCtvEKWQ','04e4f40bca711030bf72f97ad158b14972f0f2ee56d65afdf5d5b01dbb6fde02');
        // $re = $this->trc20_trade(1,'TEorNn8HmrfmDmoWNNLSUXVtLtVfVjLSX3','TS4ZqZ8cG8pAuASKS5Dr8Ue8AtrCtvEKWQ','04e4f40bca711030bf72f97ad158b14972f0f2ee56d65afdf5d5b01dbb6fde02');
        // dump($re);
        // exit();
        $usdtLogM = new \addons\member\model\MemberUsdtLog();
        $list = $usdtLogM->where(['status' => 0, 'is_platform' => 'trc20'])->select();
        $balanceM = new \addons\member\model\Balance();
        if($list){
            $with_address = config('with_trc_address');
            foreach ($list as $value){
                echo $value['id'];echo '--';
                $user_privatekey = $balanceM->where(['user_id' => $value['user_id'], 'type' => 1])->find();
                if($user_privatekey){
                    echo $user_privatekey['address']."---";
                    $re = $this->trc20_trade($value['amount'],$with_address,$user_privatekey['address'],$user_privatekey['privatekey']);
                    if(!isset($re->tx->txID)){
                        echo "error";
                        sleep(1);
                        continue;
                    }
                    echo $re->tx->txID."---end\n";
                    $re = $usdtLogM->where(['id' => $value['id']])->update(['status' => 1]);
                    if(!$re){
                    }
                    sleep(1);
                }
            }
        }
        echo 'ok';
    }


    public function sendTrx($value,$to,$from_key){
        $value = $value*bcpow(10,6);
        $credential = Credential::fromPrivateKey($from_key);
        $from = $credential->address()->base58();
        $kit = new TronKit(TronApi::mainNet(),$credential);
        return $kit->sendTrx($to,$value);
        // $ret = $inst->transfer($to,$value * 1000000);

    }

    public function trc20_trade($value,$to,$address,$from_key) {
        //USDT合约地址
        $contractAddress = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t';
        $credential = Credential::fromPrivateKey($from_key);
        $from = $credential->address()->base58();
        $kit = new TronKit(TronApi::mainNet(),$credential);
        $trx_amount = $kit->getTrxBalance($address);
        echo "|trx_amount:{$trx_amount}|";
        if($trx_amount<6){
            $res =  $this->sendTrx(6,$address,config('trc20')['private_key']);
            if(!isset($res->txid)){
                return false;
            }
            echo "|trx_hash:{$res->txid}|";
            sleep(1);
        }
        $inst = $kit->trc20($contractAddress);
        return $inst->transfer($to,$value * 1000000);
        // echo 'txid => ' . $ret->tx->txID . PHP_EOL;
        // echo 'result => ' . $ret->result . PHP_EOL;
        // die();
        // return $trc;

    }
}